package top.lyc829.zzl_android.ui.team.my_team;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.MotionEvent;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.ActivityAddTeamBinding;
import top.lyc829.zzl_android.databinding.ActivityMyTeamBinding;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.ui.profile.follow.base.FollowBaseFragment;
import top.lyc829.zzl_android.ui.profile.follow.team.FollowTeamFragment;
import top.lyc829.zzl_android.ui.profile.follow.user.FollowUserFragment;
import top.lyc829.zzl_android.ui.team.my_team.activity.ActivityFragment;
import top.lyc829.zzl_android.ui.team.my_team.chat.ChatFragment;
import top.lyc829.zzl_android.ui.team.my_team.people.PeopleFragment;
import top.lyc829.zzl_android.ui.team.my_team.travel.TravelFragment;

public class MyTeamActivity extends AppCompatActivity {

    ActivityMyTeamBinding binding;
    MyTeamViewModel mViewModel;

    private int startX = 0;
    private int startY = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMyTeamBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mViewModel = new ViewModelProvider(this).get(MyTeamViewModel.class);
        mViewModel.getTeamId().setValue(getIntent().getIntExtra("teamId", -1));

        clickListener();

        ViewPager2 viewPager2 = binding.viewPager2;
        viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                if (position == 0) {
                    return TravelFragment.newInstance();
                } else if (position == 1) {
                    return ActivityFragment.newInstance();
                } else if (position == 2) {
                    return PeopleFragment.newInstance();
                } else {
                    //return ChatFragment.newInstance();
                    return TravelFragment.newInstance();
                }
            }

            @Override
            public int getItemCount() {
                return 3;
            }
        });

        TabLayoutMediator mediator = new TabLayoutMediator(binding.tabLayout, viewPager2, ((tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("团队信息");
                    break;
                case 1:
                    tab.setText("活动信息");
                    break;
                case 2:
                    tab.setText("成员信息");
                    break;
//                case 3:
//                    tab.setText("成员交流");
//                    break;
                default:
                    break;
            }
        }));

        mediator.attach();


    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            startX = (int) ev.getX();
            startY = (int) ev.getY();
        } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            int nowX = (int) ev.getX();
            int nowY = (int) ev.getY();

            int deltaX = Math.abs(nowX - startX);
            int deltaY = Math.abs(nowY - startY);
            if (deltaX < deltaY) {
                Utils.Sout("viewpager2不滑动");
                binding.viewPager2.setUserInputEnabled(false);
            }
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            startX = 0;
            startY = 0;
            binding.viewPager2.setUserInputEnabled(true);
        }

        return super.dispatchTouchEvent(ev);
    }

    private void clickListener() {
        binding.mTitleBarMyTeam.setLeftClickListener(v -> finish());
    }

}